/**
 * Created by rockyren on 14/11/29.
 */
personalApp.directive('sidebar',['$rootScope','$state',function($rootScope,$state){
  return {
    restrict: 'EA',
    templateUrl: 'ngApp/personal/tpls/sidebar.html',
    scope: {},
    link: function(scope,element){

      var startStatusName = $state.current.name;
      var $sidebarLis = $(element).find('li');


      var highlightItem = function(targetStatusName) {

        var statusPattern = /index(\.[\w]+)?/;

        $sidebarLis.each(function(){
          var uiSref = $(this).find('a').attr('ui-sref');
          var status = uiSref.match(statusPattern);

          if(targetStatusName.indexOf(status[1]) != -1){
            $(this).addClass('active');
            return;
          }
        });
      };

      highlightItem(startStatusName);

      highlightItem(startStatusName);

      //监听状态变化事件
      $rootScope.$on('$stateChangeStart',function(evt,toState){
        $sidebarLis.removeClass('active');
        highlightItem(toState.name);
      });
    }
  }
}]);